//
//  CountingActivity.m
//  KidReading
//
//  Created by linziyuan on 16/3/7.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "CountingActivity.h"
#import "Lyt.h"
#import "DataProcessing.h"
#import "ZYCircleActivity.h"
@interface CountingActivity  ()

@property (nonatomic, assign) NSInteger currentCount;
@property (nonatomic, strong) ZYCircleActivity *wattingView;
//@property (nonatomic, strong) UIActivityIndicatorView *wattingView;
@property (nonatomic, strong) UILabel *megView;


@end

@implementation CountingActivity
{
    BOOL _isDismissing;
    CGSize _selfSize;
}
- (instancetype)initWithFrame:(CGRect)frame InView:(UIView *)view {
    if (self = [super initWithFrame:frame]) {
        _selfSize = frame.size;
        self.currentCount = 0;
        self.userInteractionEnabled = YES;
        [view addSubview:self];
        self.layer.cornerRadius = frame.size.height / 2;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0.4;
        //self.wattingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        self.wattingView.colors = @[[UIColor grayColor]];
        [self.megView setFont:[UIFont systemFontOfSize:18.0]];
        if ([DataProcessing shareSingle].deviceType == 1) {
            [self.megView setFont:[UIFont systemFontOfSize:15.0]];
        }

    }
    return self;
}



- (instancetype)initWithSize:(CGSize)size InView:(UIView *)view {
    if (self = [super init]) {
        _selfSize = size;
        self.currentCount = 0;
        self.userInteractionEnabled = YES;
        self.translatesAutoresizingMaskIntoConstraints = NO;
        [view addSubview:self];
        [self lyt_setSize:size];
        [self lyt_alignLeftToView:view margin:10];
        [self lyt_alignBottomToView:view margin:10];
        self.layer.cornerRadius = size.height / 2;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0.4;
        //self.wattingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        self.wattingView.colors = @[[UIColor grayColor]];
        [self.megView setFont:[UIFont systemFontOfSize:18.0]];
        if ([DataProcessing shareSingle].deviceType == 1) {
            [self.megView setFont:[UIFont systemFontOfSize:15.0]];
        }
        
    }
    return self;
}

- (ZYCircleActivity *)wattingView {
    if (!_wattingView) {
        CGFloat viewW = 25.0;
        CGFloat originX = _selfSize.width * 0.1;
        
        if ([DataProcessing shareSingle].deviceType == 1) {
            viewW = 20.0;
            if (_selfSize.width > 150) {
                originX = _selfSize.width * 0.05;
            }
        }else if (_selfSize.width>180) {
            originX = _selfSize.width * 0.05;
        }
        
        _wattingView = [[ZYCircleActivity alloc] initWithFrame:CGRectMake(originX, (_selfSize.height-viewW)/2, viewW, viewW)];
        _wattingView.progressLineWidth = 1.8;
        if ([DataProcessing shareSingle].deviceType == 1) {
            _wattingView.progressLineWidth = 1.0;
        }
        [self addSubview:_wattingView];
        
    }
    return _wattingView;
}

- (UILabel *)megView {
    if (!_megView) {
        _megView = [[UILabel alloc] init];
        _megView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_megView];
        [_megView lyt_alignTopToParent];
        [_megView lyt_alignBottomToParent];
        [_megView lyt_alignRightToParent];
        CGFloat leftPadding = _selfSize.width * 0.25;
       
        if ([DataProcessing shareSingle].deviceType == 1) {
            if (_selfSize.width > 150) {
                leftPadding = _selfSize.width * 0.18;
            }
        }else if (_selfSize.width>180) {
            leftPadding = _selfSize.width * 0.18;
        }
        [_megView lyt_alignLeftToView:self margin:leftPadding];
        _megView.textAlignment = NSTextAlignmentCenter;
        _megView.textColor = [UIColor whiteColor];
    }
    return _megView;
}

- (void)startShow {
    self.hidden = NO;
    self.alpha = 0.5;
    if (self.currentCount > 0) {
        self.currentCount += 1;
    }else {
        self.currentCount += 1;
        [self.wattingView startAnimating];
    }
}

- (void)showMesg:(NSString *)msg aotuDismiss:(BOOL)autodismiss {
  
    if (self.currentCount > 0) {
        self.currentCount += 1;
    }else {
        self.currentCount += 1;
        [self.wattingView startAnimating];
    }
    
    self.hidden = NO;
    self.alpha = 0.5;
    self.backgroundColor = [UIColor blackColor];
    self.wattingView.colors = @[[UIColor whiteColor]];
    if (msg && ![msg isEqualToString:@""]) {
        [self.megView setText:msg];
    }
    
    if (autodismiss) {
        [UIView animateWithDuration:2 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.currentCount = 0;
            [self.wattingView stopAnimating];
            [self.megView setText:@""];
            self.hidden = YES;
            _isDismissing = NO;
            self.backgroundColor = [UIColor clearColor];
        }];
    }
}

- (void)stopShow {
    self.currentCount -= 1;
    if (self.currentCount <= 0) {
        self.currentCount = 0;
        if (!_isDismissing) {
           [self.wattingView stopAnimating];
        }
    }
    [UIView animateWithDuration:1 animations:^{
        if (!_isDismissing) {
            self.alpha = 0.0;
        }
    } completion:^(BOOL finished) {
        self.currentCount = 0;
        if (!_isDismissing) {
            self.hidden = YES;
            [self.megView setText:@""];
            self.backgroundColor = [UIColor clearColor];
        }
        
    }];
}

- (void)dismissMesg:(NSString *)msg inSeconds:(CGFloat)second {
    //self.wattingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    _isDismissing = YES;
    self.wattingView.colors = @[[UIColor whiteColor]];
    [self.wattingView startAnimating];
    
    self.backgroundColor = [UIColor blackColor];
    if (msg && ![msg isEqualToString:@""]) {
        self.hidden = NO;
        self.alpha = 0.5;
        [self.megView setText:msg];
    }
    
    [UIView animateWithDuration:second animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        self.currentCount = 0;
        [self.wattingView stopAnimating];
        [self.megView setText:@""];
        self.hidden = YES;
        _isDismissing = NO;
        self.backgroundColor = [UIColor clearColor];
    }];
}

@end
